import { FileInfo } from '/@/services/FileService.d'
import icon from "/@/components/XUII/icon/index"

export interface FileExtInfo {
    name: string;
    fullName: string;
    size: number;
    sizeStr: string;
    lastUpdate: string;
    icon: string;
    isDir: boolean;
    ext: string;
}

const fileInfoConfig:Record<string,{icon: string}> = {
    "md": {
        icon: icon.nf_oct_markdown
    },
    "html": {
        icon: icon.nf_dev_html5
    },
    "js": {
        icon: icon.nf_dev_javascript
    },
    "css": {
        icon: icon.nf_dev_css3
    },
    "json": {
        icon: icon.nf_cod_json
    },
    "pdf": {
        icon: icon.nf_cod_file_pdf
    },
    "svg": {
        icon: icon.nf_dev_snap_svg
    },
    "zip": {
        icon: icon.nf_fa_file_zip_o
    },
    "cs": {
        icon: icon.nf_mdi_language_csharp
    },
    "ts": {
        icon: icon.nf_seti_typescript
    },
    "doc": {
        icon: icon.nf_fa_file_word_o
    },
    "docx": {
        icon: icon.nf_fa_file_word_o
    },
    "ppt": {
        icon: icon.nf_fa_file_powerpoint_o
    },
    "pptx": {
        icon: icon.nf_fa_file_powerpoint_o
    },
    "xls": {
        icon: icon.nf_fa_file_excel_o
    },
    "xlsx": {
        icon: icon.nf_fa_file_excel_o
    },
    "py": {
        icon: icon.nf_mdi_language_python_text
    },
    "epub": {
        icon: icon.nf_oct_book
    },
    "ttf": {
        icon: icon.nf_fa_font
    },
    "otf": {
        icon: icon.nf_fa_font
    },
    "txt": {
        icon: icon.nf_oct_file_text
    }
}

const getSizeShowStr = (size: number) => {
    if (!size || size <= 0) {
        return ""
      }
      if (size > 1024 * 1024 * 1024) {
        return (size / 1024 / 1024 / 1024).toFixed(2) + " GB"
      } else if (size > 1024 * 1024) {
        return (size / 1024 / 1024).toFixed(2) + " MB"
      } else if (size > 1024) {
        return (size / 1024).toFixed(2) + " KB"
      }
      return size + " B"
}
const getIcon = (f: FileInfo): string => {
    if (f.ext in fileInfoConfig) {
        let config = fileInfoConfig[f.ext]
        return config.icon
        
    }
    return f.isDir ? icon.nf_fa_folder : icon.nf_cod_file
}

const getInfo = (f: FileInfo): FileExtInfo => {
    let rs:FileExtInfo = {
        name: f.name,
        fullName: f.fullName,
        size: f.size,
        sizeStr: getSizeShowStr(f.size),
        isDir: f.isDir,
        lastUpdate: f.lastUpdate,
        icon: getIcon(f),
        ext: f.ext
    }
    return rs
}

export default {
    getInfo
}